package com.atguigu.serviceedu.controller;


import com.atguigu.commonutils.R;
import com.atguigu.serviceedu.entity.EduCourse;
import com.atguigu.serviceedu.query.CourseQuery;
import com.atguigu.serviceedu.service.EduCourseService;
import com.atguigu.servicebase.vo.CourseInfoForm;
import com.atguigu.serviceedu.vo.CoursePublishVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author wyj
 * @since 2021-12-19
 */
@RestController
@RequestMapping("/serviceedu/course")
@Api(description = "课程管理")
//@CrossOrigin //跨域
public class EduCourseController {

    @Autowired
    EduCourseService courseService;

    @PostMapping("addCourseInfo")
    @ApiOperation(value = "新增课程")
    public R addCourseInfo(
            @ApiParam(name = "CourseInfoForm", value = "课程基本信息", required = true)
            @RequestBody CourseInfoForm courseInfoForm) {
        courseService.addCourseInfo(courseInfoForm);
        //查询课程id
        EduCourse eduCourse = courseService.getOne(new QueryWrapper<EduCourse>().eq("title", courseInfoForm.getTitle()));;

        return R.ok().data("courseId",eduCourse.getId());
    }

    //根据课程id查询课程基本信息
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId) {
        CourseInfoForm courseInfoVo = courseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoVo", courseInfoVo);
    }

    //修改课程信息
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoForm courseInfoVo) {
        courseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }

    //根据课程id查询课程确认信息
    @GetMapping("getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable(value = "id") String id) {
        CoursePublishVo coursePublishVo = courseService.publishCourseInfo(id);
        return R.ok().data("publishCourse", coursePublishVo);
    }

    //分页查询课程信息为列表
    @PostMapping("Condition/{page}/{limit}")
    public R publishCourse(@ApiParam(name = "page", value = "当前页码", required = true)
                           @PathVariable Long page,

                           @ApiParam(name = "limit", value = "每页记录数", required = true)
                           @PathVariable Long limit,

                           @ApiParam(name = "teacherQuery", value = "查询对象", required = false)
                           @RequestBody(required = false) CourseQuery courseQuery) {
        Page<EduCourse> pageParam = new Page<>(page, limit);
        courseService.CoursePageQuery(pageParam,courseQuery);
        List<EduCourse> records = pageParam.getRecords();
        long total = pageParam.getTotal();
        return R.ok().data("list",records).data("total",total);
    }
    //根据课程id查询课程确认信息
    @DeleteMapping("deleteCourse/{id}")
    public R deleteCourseById(@PathVariable(value = "id") String id) {
        courseService.removeCourseById(id);
        return R.ok();
    }

    @PostMapping("publishCourse/{id}")
    public R publishCourseById(@PathVariable(value = "id")String CourseId){
        courseService.updateCourseStatusById(CourseId);
        return R.ok();
    }
}

